﻿using Kylin.Wiki.Core.Const;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Kylin.Wiki.Core.Extensions;
using Microsoft.Extensions.Options;

namespace Kylin.Wiki.Core.Configuration
{
    /// <summary>
    /// 全局配置参数类
    /// </summary>
    public static class AppSetting
    {
        /// <summary>
        /// 配置管理类
        /// </summary>
        public static IConfiguration Configuration { get; private set; }

        /// <summary>
        /// 数据库连接帮助类
        /// </summary>
        private static DBConnection _connection;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string DbConnectionString
        {
            get { return _connection.DbConnectionString; }
        }

        /// <summary>
        /// Redis连接字符串
        /// </summary>
        public static string RedisConnectionString
        {
            get { return _connection.RedisConnectionString; }
        }

        /// <summary>
        /// 是否使用Redis缓存
        /// </summary>
        public static bool UseRedis
        {
            get { return _connection.UseRedis; }
        }

        /// <summary>
        /// JWT加密Key
        /// </summary>
        public static JWTSecret Secret { get; private set; }

        /// <summary>
        /// Token 前缀
        /// </summary>
        public static string TokenHeaderName = "Authorization";

        /// <summary>
        /// 默认上传文件大小限制3M
        /// </summary>
        public static int LimitUpFileSizee { get; set; } = 3;

        /// <summary>
        /// 当前目录
        /// </summary>
        public static string CurrentPath { get; private set; } = null;

        /// <summary>
        /// 文件所在路径
        /// </summary>
        public static string VirtualPath { get; private set; }

        /// <summary>
        /// 初始化参数配置
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void Init(IServiceCollection services, IConfiguration configuration)
        {
            Configuration = configuration;
            services.Configure<JWTSecret>(configuration.GetSection("Secret"));
            services.Configure<DBConnection>(configuration.GetSection("Connection")); 

            var provider = services.BuildServiceProvider();
            IWebHostEnvironment environment = provider.GetRequiredService<IWebHostEnvironment>();
            CurrentPath = Path.Combine(environment.ContentRootPath, "").ReplacePath();
            Secret = provider.GetRequiredService<IOptions<JWTSecret>>().Value;
            _connection = provider.GetRequiredService<IOptions<DBConnection>>().Value;

            if (string.IsNullOrEmpty(_connection.DbConnectionString))
                throw new System.Exception("未配置好数据库默认连接");

            VirtualPath = (configuration["VirtualPath"] ?? CurrentPath).ToString();
        }
    }

}
